package cn.tedu._05interceptor.base.config;

import cn.tedu._05interceptor.base.interceptors.MyInterceptor;
import cn.tedu._05interceptor.base.interceptors.MyInterceptor2;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * WebMvcConfigurer接口:
 *   1.Spring MVC提供的一个标准化配置接口;
 *   2.可以配置拦截器、可以配置跨域、可以配置...;
 */
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
    //addI+回车
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //注册拦截器,并添加拦截路径;
        //拦截策略1：拦截所有请求;
        //registry.addInterceptor(new MyInterceptor())
        //        .addPathPatterns("/v1/**");

        //拦截策略2：拦截指定的单个请求;
        //registry.addInterceptor(new MyInterceptor())
        //        .addPathPatterns("/v1/user/login");

        //拦截策略3：拦截指定的多个请求,比如拦截登录和注册两个请求;
        //registry.addInterceptor(new MyInterceptor())
        //        .addPathPatterns("/v1/user/login", "/v1/user/reg");

        //拦截策略4: 拦截指定模块的所有请求,比如拦截用户模块的所有请求;
        //registry.addInterceptor(new MyInterceptor())
        //        .addPathPatterns("/v1/user/**");

        //拦截策略5: 排除指定的请求,比如拦截用户模块中除了登录之外的所有请求;
        //registry.addInterceptor(new MyInterceptor())
        //        .addPathPatterns("/v1/user/**")
        //        .excludePathPatterns("/v1/user/login");

        //多个拦截器的执行顺序
        /*
            preHandle是按照注册拦截器的顺序依次执行;
            postHandle和afterCompletion是按照注册拦截器的相反的顺序依次执行;

            list01: [preHandle1, preHandle2]
            list02: [postHandle1, postHandle2]
            list03: [afterCompletion1, afterCompletion2]
         */
        registry.addInterceptor(new MyInterceptor())
                .addPathPatterns("/v1/**");
        registry.addInterceptor(new MyInterceptor2())
                .addPathPatterns("/v1/**");
    }
}















